//
//  AddBankCardViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/6.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "AddBankCardViewModel.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "AddBankCardTableViewCell.h"
#import "BankCardlistHeaderView.h"
#import "BankFooterView.h"
#import "BankCardModel.h"
#import "LoginModel.h"

@interface AddBankCardViewModel ()<UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate>
{
    UITableView *selfTableView;
    NSArray *leftStrArr;
    NSArray *rightStrArr;
    BankCardDetailModel *model;
}

@end

@implementation AddBankCardViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        leftStrArr = @[@"姓名", @"开户银行名称", @"银行卡号"];
        rightStrArr = @[@"请输入银行开卡人姓名", @"请输入开户银行名称", @"请输银行卡号"];

        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        tableView.sectionFooterHeight = 0.0;
        tableView.sectionHeaderHeight = 0.0;
        [tableView registerClass:[BankCardlistHeaderView class] forHeaderFooterViewReuseIdentifier:@"BankCardlistHeaderView"];
        [tableView registerClass:[BankFooterView class] forHeaderFooterViewReuseIdentifier:@"BankFooterView"];
        [superView addSubview:tableView];
        selfTableView = tableView;
        
        model = [BankCardDetailModel new];
    }
    return self;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(111);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    BankCardlistHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"BankCardlistHeaderView"];
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    BankFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"BankFooterView"];
    kWeakSelf(self);
    footer.BlockApplyWithDraw = ^(GLGWButton * _Nullable applyWithDraw) {
        [weakself btnKeep:applyWithDraw];
    };
    footer.btnStr = @"保存";
    return footer;
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return leftStrArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kHeightProportion_In_iPhoneTriploid(44);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddBankCardTableViewCell *detailCell = [AddBankCardTableViewCell cellWithTableView:tableView];
    detailCell.leftStr = leftStrArr[indexPath.row];
    [self textfildSetting:detailCell.textFild index:indexPath];
    
    return detailCell;
}

- (void)textfildSetting:(GLGWTextField *)textF index:(NSIndexPath *)indexP
{
    textF.placeholder = rightStrArr[indexP.row];
    textF.delegate = self;
    if (indexP.row == 2) {
        textF.keyboardType = UIKeyboardTypeNumberPad;
    } else {
        textF.keyboardType = UIKeyboardTypeDefault;
    }
    textF.tag = 565 + indexP.row;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *str = [NSString stringWithTextFiledStr:textField.text replacementString:string];
    DLog(@"%@",str);
    if (textField.tag == 565) {
        model.realName = str;
    } else if (textField.tag == 566) {
        model.bankName = str;
    } else if (textField.tag == 567) {
        model.cardNum = str;
        //银联标准卡卡号长度一般是在16-19位，开头以62开头。添加银行卡只能添加借记卡
    }
    return YES;
}

- (void)setType:(AddOrEditType)type
{
    _type = type;
    if (type == AddOrEditType_Defat) {  // 刷新
        
    }
}

- (void)btnKeep:(GLGWButton *)sender
{
    // 添加请求保存接口
    if (model.realName.length <= 0) {
        DLog(@"名字不能为空");
        return;
    }
    if (model.bankName.length <= 0) {
        DLog(@"开户行名称不能为空");
        return;
    }
    if (model.cardNum.length > 19 || model.cardNum.length < 16) {
        DLog(@"银行卡号位数为16-19位");
        return;
    }
    NSString *str = [model.cardNum substringToIndex:2];
    if (![str isEqualToString:@"62"]) {
        DLog(@"银行卡号以62开头");
        return;
    }
    [self requestAddBank];
}


#pragma mark - 添加银行卡接口
- (void)requestAddBank
{
    if (self.userInforData.length == 0) return;
    LoginModel *modles = [NSKeyedUnarchiver unarchiveObjectWithData:self.userInforData];
    NSDictionary *dic = @{
                          @"userId" : koString(modles.data.userId),
                          @"realName" : koString(model.realName),             // 用户名/企业名
                          @"bankName" : koString(model.bankName),             // 账户
                          @"cardNum" : koString(model.cardNum),               // 卡号
                          @"isPhone" : @(2),                                  // 非pc
                          };
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kAddBank] lodingView:nil lodingTextString:@"" isLoding:YES parameters:dic success:^(id  _Nullable response) {
        BaseModel *baseModel = [BaseModel yy_modelWithJSON:response];
        if (baseModel.code == 0) {
            if (self.BlockKeepCard) {
                self.BlockKeepCard(self->model);
            }
        } else {
            DLog(@"%@",baseModel.message);
        }

    } failure:^(NSError * _Nullable error) {
        DLog(@"添加银行卡:%@",error);
    }];
}

@end
